package com.ddl.service.impl;

import com.ddl.common.exception.ResultModel;
import com.ddl.dao.MoneyRecordMapper;
import com.ddl.dao.StagePropertyMapper;
import com.ddl.dao.UserMapper;
import com.ddl.dao.UserStagePropertyMapper;
import com.ddl.enums.StagePropertyTypeEnum;
import com.ddl.pojo.MoneyRecord;
import com.ddl.pojo.StageProperty;
import com.ddl.pojo.User;
import com.ddl.pojo.UserStageProperty;
import com.ddl.service.MoneyRecordService;
import com.ddl.service.StagePropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: liyuxiang
 * @create: 2018-12-01
 */
@Service
public class StagePropertyServiceImpl implements StagePropertyService {

    @Autowired
    StagePropertyMapper stagePropertyMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserStagePropertyMapper userStagePropertyMapper;

    @Autowired
    MoneyRecordService moneyRecordService;

    @Override
    public ResultModel getPropList() {
        List<StageProperty> stagePropertyList = stagePropertyMapper.getAll();
        Map<String, Object> map = new HashMap<>();
        map.put("videoList", stagePropertyList);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(map);
        return resultModel;
    }

    @Override
    public ResultModel submitOrder(String userId, String propertyId, int count) {
        ResultModel resultModel = new ResultModel();
        User user = userMapper.selectByPrimaryKey(Integer.parseInt(userId));
        StageProperty stageProperty = stagePropertyMapper.selectByPrimaryKey(Integer.parseInt(propertyId));
        List<UserStageProperty> userStagePropertyList = userStagePropertyMapper.getListByUserId(Integer.parseInt(userId));
        BigDecimal total = stageProperty.getPrice().multiply(new BigDecimal(count));

        if (StagePropertyTypeEnum.HONG_JIU_MALL.getType().equalsIgnoreCase(stageProperty.getType())) {
            resultModel = ResultModel.getErrorInstance("红酒仅展示，不能在线购买");
            return resultModel;
        }

        if (total.compareTo(user.getTotalAmount()) > 0) {
            resultModel = ResultModel.getErrorInstance("金币不足");
            return resultModel;
        }

        if (StagePropertyTypeEnum.isDog(stageProperty.getType())) {
            if (count > 1) {
                resultModel = ResultModel.getErrorInstance("一次只能购买一只狗");
                return resultModel;
            }

            List<StageProperty> stagePropertyList = stagePropertyMapper.getAll();
            for (UserStageProperty userStageProperty : userStagePropertyList) {
                for (StageProperty property : stagePropertyList) {
                    if (property.getId().equals(userStageProperty.getStagePropertyId())) {
                        if (StagePropertyTypeEnum.isDog(property.getType()) && userStageProperty.getCount() > 0) {
                            resultModel = ResultModel.getErrorInstance("您已经有一只狗了，请使用后再次购买");
                            return resultModel;
                        }
                    }
                }
            }
        }

        //写入我的道具表数据
        boolean isAdd = false;
        if (!CollectionUtils.isEmpty(userStagePropertyList)) {
            for (UserStageProperty userStageProperty : userStagePropertyList) {
                if (userStageProperty.getStagePropertyId() == Integer.parseInt(propertyId)) {
                    userStageProperty.setCount(userStageProperty.getCount() + count);
                    userStageProperty.setUpdateTime(new Date());
                    userStagePropertyMapper.updateByPrimaryKey(userStageProperty);
                    isAdd = true;
                }
            }
        }
        if (!isAdd) {
            UserStageProperty userStageProperty = new UserStageProperty();
            userStageProperty.setStagePropertyId(Integer.parseInt(propertyId));
            userStageProperty.setCreateTime(new Date());
            userStageProperty.setUserId(Integer.parseInt(userId));
            userStageProperty.setCount(count);
            userStageProperty.setUpdateTime(new Date());
            userStageProperty.setIsDeleted(0);
            userStagePropertyMapper.insert(userStageProperty);
        }

        //写入消费记录表
        moneyRecordService.consumer(Integer.parseInt(userId), total, "购买道具：" + propertyId + "," + count + "个");

        user.setTotalAmount(user.getTotalAmount().subtract(total));
        userMapper.updateByPrimaryKey(user);

        resultModel = ResultModel.getSuccessInstance();
        resultModel.setData("购买成功，共花费金币" + total + "，购买了" + count + "个" + stageProperty.getName());
        return resultModel;
    }

    @Override
    public ResultModel geUserPropList(int userId) {
        List<UserStageProperty> allUserStagePropertyList = userStagePropertyMapper.getListByUserId(userId);
        List<StageProperty> stagePropertyList = stagePropertyMapper.getAll();
        Map<Integer, StageProperty> stagePropertyMap = stagePropertyList.stream().collect(Collectors.toMap(StageProperty::getId, stageProperty -> stageProperty));
        List<Map<String, Object>> userStagePropertyList = new ArrayList<>(allUserStagePropertyList.size());
        for (UserStageProperty userStageProperty : allUserStagePropertyList) {
            if (userStageProperty.getCount() > 0) {
                StageProperty stageProperty = stagePropertyMap.get(userStageProperty.getStagePropertyId());
                Map<String, Object> map = new HashMap<>();
                map.put("name", stageProperty.getName());
                map.put("headImage", stageProperty.getHeadImage());
                map.put("type", stageProperty.getType());
                map.put("price", stageProperty.getPrice());
                map.put("count", userStageProperty.getCount());
                userStagePropertyList.add(map);
            }
        }
        ResultModel resultModel = ResultModel.getSuccessInstance();
        Map<String, Object> result = new HashMap<>();
        result.put("userStagePropertyList", userStagePropertyList);
        resultModel.setData(result);
        return resultModel;
    }
}
